<?php

error_reporting(E_ALL);
ob_start();
include_once "../connect.php";
// รับค่ามาเก็บไว้ใน 
$cn_id = $_POST["cn_id"];
$c_id = $_POST["c_id"];
$oth_id = $_POST["oth_id"];
$oth_id2 = $_POST["oth_id2"];
$oth_id3 = $_POST["oth_id3"];
$cn_number = $_POST["cn_number"];
$cn_comeName = $_POST["cn_comeName"];
$de_id = $_POST["de_id"];
$cn_ip = $_POST["cn_ip"];
$id_member = $_POST["id_member"];
$c_mb = $_POST["c_mb"];
$c_vga = $_POST["c_vga"];
$c_cpu = $_POST["c_cpu"];
$c_fdd = $_POST["c_fdd"];
$c_ram = $_POST["c_ram"];
$c_hdd = $_POST["c_hdd"];
$c_sound = $_POST["c_sound"];
$c_cdRom = $_POST["c_cdRom"];
$c_speaker = $_POST["c_speaker"];
$c_lan = $_POST["c_lan"];
$c_notic = $_POST["c_notic"];
$oth_name = $_POST["oth_name"];
$oth_detail = $_POST["oth_detail"];
$emp_id = $_POST["emp_id"];
$c_keyboard = $_POST["c_keyboard"];
$c_mouse = $_POST["c_mouse"];
$c_monitor = $_POST["c_monitor"];
$oth_name2 = $_POST["oth_name2"];
$oth_detail2 = $_POST["oth_detail2"];
$oth_name3 = $_POST["oth_name3"];
$oth_detail3 = $_POST["oth_detail3"];



//$c_nameUser= $_POST["c_nameUser"];
//$hid_department = $_POST["hid_department"];

$sqlCnNumber = " select * from comname where cn_number = '$cn_number' and cn_id != '$cn_id' ";
$CN_Number = mysql_query($sqlCnNumber);
if (mysql_num_rows($CN_Number) != 0) {
    echo "	<script language='javascript'>
		alert('ชื่อเครื่องซ้ำครับ');
		setTimeout(\"parent.$.fancybox.close()\",1000);
		</script>";
    exit();
}

if (!empty($cn_id)) {

    //selec db for check update
    $exeComputer = "SELECT * FROM computer LEFT  JOIN comother  ON computer.cn_id = comother.cn_id      
                        WHERE  computer.cn_id = '$cn_id'";
    $rsExeComputer = mysql_query($exeComputer);
    $rExeComputer = mysql_fetch_array($rsExeComputer);

    $cnNumber = $rExeComputer['cn_number'];
    $keyBoad = $rExeComputer['c_keyboard'];
    $mouse = $rExeComputer['c_mouse'];
    $monitor = $rExeComputer['c_monitor'];


    //update for donate
    if ($cnNumber != $cn_number) {
        $upDateProDe1 = "UPDATE product_detail SET pd_status = 'donate' WHERE pd_id = '$cnNumber'";
        mysql_query($upDateProDe1);

        $upComNum = "UPDATE computer SET cn_number = '$cn_number' WHERE cn_id = '$cn_id'";
        mysql_query($upComNum);

        $upComnameNum = "UPDATE comname SET cn_number = '$cn_number' WHERE cn_id = '$cn_id'";
        mysql_query($upComnameNum);


//update for new device   
        $upCom = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$cn_number'";
        mysql_query($upCom);


        //update  product   Balance
        $exeCom = "SELECT * FROM product_detail WHERE pd_id = '$cn_number'";
        $rs = mysql_query($exeCom);
        if ($rs) {
            $r = mysql_fetch_array($rs);
            $Prd_id = $r['prd_id'];
            $exeComDe = "SELECT * FROM product WHERE prd_id = '$Prd_id'";
            $rsComD = mysql_query($exeComDe);

            $rComDe = mysql_fetch_array($rsComD);
            $Prd_balance = $rComDe['prd_balance'];
            $upComDe = "UPDATE product SET prd_balance = '$Prd_balance' - '1' WHERE prd_id = '$Prd_id'  ";

            mysql_query($upComDe);
        } else {
            echo mysql_error();
        }
    }

    //check compare device for update
    if ($keyBoad != $c_keyboard) {
        $upProdeKey = "UPDATE product_detail SET pd_status = 'donate' WHERE pd_id = '$keyBoad'";
        mysql_query($upProdeKey);

        $upCopuKey = "UPDATE  computer SET c_keyboard = '$c_keyboard' WHERE cn_id = '$cn_id'";
        mysql_query($upCopuKey);
//status IsUse
        $upKey = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$c_keyboard'";
        mysql_query($upKey);

        //Update Product For Balance
        $exeKeyDe = "SELECT * FROM product_detail WHERE pd_id = '$c_keyboard'";
        $rsKeyDe = mysql_query($exeKeyDe);
        if ($rsKeyDe) {
            $rKeyDe = mysql_fetch_array($rsKeyDe);
            $Prd_idKey = $rKeyDe['prd_id'];
            $exeKey = "SELECT * FROM product WHERE prd_id = '$Prd_idKey'";
            $rsKey = mysql_query($exeKey);

            $rKey = mysql_fetch_array($rsKey);
            $Prd_balKey = $rKey['prd_balance'];

            $upKey = "UPDATE product SET prd_balance = '$Prd_balKey' - '1' WHERE prd_id = '$Prd_idKey' ";
            mysql_query($upKey);
        } else {
            echo mysql_error();
        }
    }


    if ($mouse != $c_mouse) {
        $upProMouse = "UPDATE product_detail SET pd_status = 'donate' WHERE pd_id = '$mouse'";
        mysql_query($upProMouse);

        $upCompuMouse = "UPDATE computer SET c_mouse = '$c_mouse' WHERE cn_id = '$cn_id' ";
        mysql_query($upCompuMouse);

        $upMou = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$c_mouse'";
        mysql_query($upMou);

        $exeMousDe = "SELECT * FROM product_detail WHERE pd_id = '$c_mouse'";
        $rsMouseDe = mysql_query($exeMousDe);
        if ($rsMouseDe) {
            $rMousDe = mysql_fetch_array($rsMouseDe);
            $prd_idMouse = $rMousDe['prd_id'];
            $exeMouse = "SELECT * FROM product WHERE prd_id = '$prd_idMouse' ";
            $rsMouse = mysql_query($exeMouse);

            $rMouse = mysql_fetch_array($rsMouse);
            $Prd_balanceMou = $rMouse['prd_balance'];

            $upMouseBal = "UPDATE  product SET prd_balance = '$Prd_balanceMou' - '1' WHERE prd_id = '$prd_idMouse'";
            mysql_query($upMouseBal);
        } else {
            echo mysql_error();
        }
    }

    if ($monitor != $c_monitor) {
        $upProMoni = "UPDATE product_detail SET pd_status = 'donate' WHERE pd_id = '$monitor'";
        mysql_query($upProMoni);

        $upCompuMoni = "UPDATE computer SET c_monitor = '$c_monitor' WHERE cn_id = '$cn_id' ";
        mysql_query($upCompuMoni);

        $upMoni = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$c_monitor'";
        mysql_query($upMoni);

        $exeMoniDe = "SELECT * FROM product_detail WHERE pd_id = '$c_monitor'";
        $rsMoniDe = mysql_query($exeMoniDe);

        if ($rsMoniDe) {
            $rMoniDe = mysql_fetch_array($rsMoniDe);
            $Prd_idMoniDe = $rMoniDe['prd_id'];
            $exeMoni = "SELECT * FROM product WHERE prd_id = '$Prd_idMoniDe'";
            $rsMoni = mysql_query($exeMoni);
            $rMoni = mysql_fetch_array($rsMoni);
            $Prd_balanceMoni = $rMoni['prd_balance'];

            $upMoniBalan = "UPDATE product SET prd_balance = '$Prd_balanceMoni' - '1' WHERE prd_id = '$Prd_idMoniDe'";

            mysql_query($upMoniBalan);
        } else {
            echo mysql_error();
        }
    }
}

if (!empty($oth_id)) {
    $upOther = "UPDATE comother SET cn_number = '$cn_number'  WHERE `comother`.`oth_id` ='$oth_id' ";
    mysql_query($upOther);

    $exeOther = "SELECT * FROM comname
                                            LEFT JOIN depart ON comname.de_id = depart.id
                                            LEFT JOIN computer ON comname.cn_id = computer.cn_id
                                            LEFT JOIN comother ON comname.cn_id = comother.cn_id
                                            LEFT JOIN employee ON comname.emp_id = employee.emp_id
                                            LEFT JOIN product_detail ON comother.oth_detail = product_detail.pd_id
                                            LEFT JOIN product ON product_detail.prd_id = product.prd_id
                                            WHERE comname.cn_id ='$cn_id'
                                            AND type_id = '6' ";
    $rsOther = mysql_query($exeOther);
    $rOther = mysql_fetch_array($rsOther);

    $exeOtherDetail = $rOther['oth_detail'];

    $upComOther = "UPDATE comother SET `oth_name` = '$oth_name', `emp_id` = '$emp_id',  cn_number = '$cn_number'  WHERE `comother`.`oth_id` ='$oth_id' ";

    mysql_query($upComOther);

    if ($exeOtherDetail != $oth_detail) {
        $upProComoth = "UPDATE product_detail SET pd_status = 'donate' WHERE pd_id = '$exeOtherDetail'";
        mysql_query($upProComoth);

        $upCompuComoth = "UPDATE comother SET oth_detail = '$oth_detail' WHERE oth_id = '$oth_id' ";
        mysql_query($upCompuComoth);

        $upComoth = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$oth_detail'";
        mysql_query($upComoth);

        $exeComoth = "SELECT * FROM product_detail WHERE pd_id = '$oth_detail'";
        $rsComoth = mysql_query($exeComoth);

        if ($rsComoth) {
            $rComothDe = mysql_fetch_array($rsComoth);
            $Prd_idComoth = $rComothDe['prd_id'];
            $exeComoth = "SELECT * FROM product WHERE prd_id = '$Prd_idComoth'";
            $rsComOth = mysql_query($exeComoth);
            $rComoth = mysql_fetch_array($rsComOth);
            $Prd_balanceComoth = $rComoth['prd_balance'];

            $upComothBalan = "UPDATE product SET prd_balance = '$Prd_balanceComoth' - '1' WHERE prd_id = '$Prd_idComoth'";

            mysql_query($upComothBalan);
        } else {
            echo mysql_error();
        }
    }
} else if ($oth_detail != NULL) {
    $sqlOther = "
    INSERT INTO `db_itmanager`.`comother` (
`oth_name` ,
`oth_detail` ,
`cn_number` ,
`cn_id` ,
`emp_id` 
)
VALUES (
'$oth_name', '$oth_detail', '$cn_number' , '$cn_id', '$emp_id'
)";
    mysql_query($sqlOther);

    $upOth = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$oth_detail'";
    mysql_query($upOth);

    $exeOthde = "SELECT * FROM product_detail WHERE pd_id = '$oth_detail'";
    $rsOthde = mysql_query($exeOthde);
    if ($rsOthde) {
        $rOthde = mysql_fetch_array($rsOthde);
        $Prd_idOthde = $rOthde['prd_id'];

        $exeOth = "SELECT * FROM product WHERE prd_id = '$Prd_idOthde'";
        $rsOth = mysql_query($exeOth);
        $rOth = mysql_fetch_array($rsOth);
        $Prd_balOth = $rOth['prd_balance'];

        $upOth = "UPDATE product SET prd_balance = '$Prd_balOth' - '1' WHERE prd_id = '$Prd_idOthde' ";
        mysql_query($upOth);
    }
}

if (!empty($oth_id2)) {
    $upOther2 = "UPDATE comother SET cn_number = '$cn_number'  WHERE `comother`.`oth_id` ='$oth_id2' ";
    mysql_query($upOther2);

    $exeOther2 = "SELECT * FROM comname
                                            LEFT JOIN depart ON comname.de_id = depart.id
                                            LEFT JOIN computer ON comname.cn_id = computer.cn_id
                                            LEFT JOIN comother ON comname.cn_id = comother.cn_id
                                            LEFT JOIN employee ON comname.emp_id = employee.emp_id
                                            LEFT JOIN product_detail ON comother.oth_detail = product_detail.pd_id
                                            LEFT JOIN product ON product_detail.prd_id = product.prd_id
                                            WHERE comname.cn_id ='$cn_id'
                                            AND type_id = '5' ";
    $rsOther2 = mysql_query($exeOther2);
    $rOther2 = mysql_fetch_array($rsOther2);

    $exeOtherDetail2 = $rOther2['oth_detail'];

    $upComOther2 = "UPDATE comother SET `oth_name` = '$oth_name2', `emp_id` = '$emp_id',  cn_number = '$cn_number'  WHERE `comother`.`oth_id` ='$oth_id2' ";

    mysql_query($upComOther2);

    if ($exeOtherDetail2 != $oth_detail2) {
        $upProComoth2 = "UPDATE product_detail SET pd_status = 'donate' WHERE pd_id = '$exeOtherDetail2'";
        mysql_query($upProComoth2);

        $upCompuComoth2 = "UPDATE comother SET oth_detail = '$oth_detail2' WHERE oth_id = '$oth_id2' ";
        mysql_query($upCompuComoth2);

        $upComoth2 = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$oth_detail2'";
        mysql_query($upComoth2);

        $exeComoth2 = "SELECT * FROM product_detail WHERE pd_id = '$oth_detail2'";
        $rsComoth2 = mysql_query($exeComoth2);

        if ($rsComoth2) {
            $rComothDe2 = mysql_fetch_array($rsComoth2);
            $Prd_idComoth2 = $rComothDe2['prd_id'];
            $exeComoth2 = "SELECT * FROM product WHERE prd_id = '$Prd_idComoth2'";
            $rsComOth2 = mysql_query($exeComoth2);
            $rComoth2 = mysql_fetch_array($rsComOth2);
            $Prd_balanceComoth2 = $rComoth2['prd_balance'];

            $upComothBalan2 = "UPDATE product SET prd_balance = '$Prd_balanceComoth2' - '1' WHERE prd_id = '$Prd_idComoth2'";

            mysql_query($upComothBalan2);
        } else {
            echo mysql_error();
        }
    }
} else if ($oth_detail2 != NULL) {
    $sqlOther2 = "
    INSERT INTO `db_itmanager`.`comother` (
`oth_name` ,
`oth_detail` ,
`cn_number` ,
`cn_id` ,
`emp_id` 
)
VALUES (
'$oth_name2', '$oth_detail2', '$cn_number' , '$cn_id', '$emp_id'
)";
    mysql_query($sqlOther2);

    $upOth2 = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$oth_detail2'";
    mysql_query($upOth2);

    $exeOthde2 = "SELECT * FROM product_detail WHERE pd_id = '$oth_detail2'";
    $rsOthde2 = mysql_query($exeOthde2);
    if ($rsOthde2) {
        $rOthde2 = mysql_fetch_array($rsOthde2);
        $Prd_idOthde2 = $rOthde2['prd_id'];

        $exeOth2 = "SELECT * FROM product WHERE prd_id = '$Prd_idOthde2'";
        $rsOth2 = mysql_query($exeOth2);
        $rOth2 = mysql_fetch_array($rsOth2);
        $Prd_balOth2 = $rOth2['prd_balance'];

        $upOth2 = "UPDATE product SET prd_balance = '$Prd_balOth2' - '1' WHERE prd_id = '$Prd_idOthde2' ";
        mysql_query($upOth2);
    }
}

if (!empty($oth_id3)) {
    $upOther3 = "UPDATE comother SET cn_number = '$cn_number'  WHERE `comother`.`oth_id` ='$oth_id3' ";
    mysql_query($upOther3);

    $exeOther3 = "SELECT * FROM comname
                                            LEFT JOIN depart ON comname.de_id = depart.id
                                            LEFT JOIN computer ON comname.cn_id = computer.cn_id
                                            LEFT JOIN comother ON comname.cn_id = comother.cn_id
                                            LEFT JOIN employee ON comname.emp_id = employee.emp_id
                                            LEFT JOIN product_detail ON comother.oth_detail = product_detail.pd_id
                                            LEFT JOIN product ON product_detail.prd_id = product.prd_id
                                            WHERE comname.cn_id ='$cn_id'
                                            AND type_id = '8' ";
    $rsOther3 = mysql_query($exeOther3);
    $rOther3 = mysql_fetch_array($rsOther3);

    $exeOtherDetail3 = $rOther3['oth_detail'];

    $upComOther3 = "UPDATE comother SET `oth_name` = '$oth_name3', `emp_id` = '$emp_id',  cn_number = '$cn_number'  WHERE `comother`.`oth_id` ='$oth_id3' ";

    mysql_query($upComOther3);

    if ($exeOtherDetail3 != $oth_detail3) {
        $upProComoth3 = "UPDATE product_detail SET pd_status = 'donate' WHERE pd_id = '$exeOtherDetail3'";
        mysql_query($upProComoth3);

        $upCompuComoth3 = "UPDATE comother SET oth_detail = '$oth_detail3' WHERE oth_id = '$oth_id3' ";
        mysql_query($upCompuComoth3);

        $upComoth3 = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$oth_detail3'";
        mysql_query($upComoth3);

        $exeComoth3 = "SELECT * FROM product_detail WHERE pd_id = '$oth_detail3'";
        $rsComoth3 = mysql_query($exeComoth3);

        if ($rsComoth3) {
            $rComothDe3 = mysql_fetch_array($rsComoth3);
            $Prd_idComoth3 = $rComothDe3['prd_id'];
            $exeComoth3 = "SELECT * FROM product WHERE prd_id = '$Prd_idComoth3'";
            $rsComOth3 = mysql_query($exeComoth3);
            $rComoth3 = mysql_fetch_array($rsComOth3);
            $Prd_balanceComoth3 = $rComoth3['prd_balance'];

            $upComothBalan3 = "UPDATE product SET prd_balance = '$Prd_balanceComoth3' - '1' WHERE prd_id = '$Prd_idComoth3'";

            mysql_query($upComothBalan3);
        } else {
            echo mysql_error();
        }
    }
} else if ($oth_detail3 != NULL) {
    $sqlOther3 = "
    INSERT INTO `db_itmanager`.`comother` (
`oth_name` ,
`oth_detail` ,
`cn_number` ,
`cn_id` ,
`emp_id` 
)
VALUES (
'$oth_name3', '$oth_detail3', '$cn_number' , '$cn_id', '$emp_id'
)";
    mysql_query($sqlOther3);

    $upOth3 = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_id` ='$oth_detail3'";
    mysql_query($upOth3);

    $exeOthde3 = "SELECT * FROM product_detail WHERE pd_id = '$oth_detail3'";
    $rsOthde3 = mysql_query($exeOthde3);
    if ($rsOthde3) {
        $rOthde3 = mysql_fetch_array($rsOthde3);
        $Prd_idOthde3 = $rOthde3['prd_id'];

        $exeOth3 = "SELECT * FROM product WHERE prd_id = '$Prd_idOthde3'";
        $rsOth3 = mysql_query($exeOth3);
        $rOth3 = mysql_fetch_array($rsOth3);
        $Prd_balOth3 = $rOth3['prd_balance'];

        $upOth3 = "UPDATE product SET prd_balance = '$Prd_balOth3' - '1' WHERE prd_id = '$Prd_idOthde3' ";
        mysql_query($upOth3);
    }
}


if (!empty($cn_id)) {

    $exeComnameOld = "SELECT * FROM comname WHERE cn_id = '$cn_id' ";
    $rsComnameOld = mysql_query($exeComnameOld);
    $rComnameOld = mysql_fetch_array($rsComnameOld);
    $empOld = $rComnameOld['emp_id'];

    $upEmployee = "UPDATE employee SET cn_id = NULL WHERE emp_id = $empOld ";

    mysql_query($upEmployee);
    
    $upEmpNew = "UPDATE employee SET cn_id = '$cn_id' WHERE emp_id = '$emp_id' ";
    
    mysql_query($upEmpNew);


    $sql = "
UPDATE  comname  SET
        cn_comeName = '$cn_comeName',   
        de_id = '$de_id',
cn_ip = '$cn_ip' ,
    id_member = '$id_member',
         `emp_id` = '$emp_id',
     cn_createdate =  NOW()
 WHERE cn_id = '$cn_id' ";



    //mysql_query(sql1);
    $sql2 = "
UPDATE `db_itmanager`.`computer` SET 
`c_mb` = '$c_mb',
`c_cpu` = '$c_cpu',
`c_ram` = '$c_ram',
`c_hdd` = '$c_hdd',
`c_sound` = '$c_sound',
`c_cdRom` = '$c_cdRom',
`c_lan` = '$c_lan',
`c_vga` = '$c_vga',
`c_fdd` = '$c_fdd',
`c_speaker` = '$c_speaker',
`c_notic` = '$c_notic',
    `emp_id` = '$emp_id'
    WHERE `computer`.`c_id` ='$c_id' ";
    
    mysql_query($sql);
    
} else {
    echo mysql_error();
}
if (mysql_query($sql2)) {


    // header("location:listCom.php");
    echo "<script language='javascript'>
			   alert('Complete');
			   setTimeout(\"parent.$.fancybox.close()\",1000); 
			  </script>";
    echo "<meta http-equiv='refresh' content='5; url=listCom.php'>";
} else {
    echo mysql_error();
}
?>
